package soendtrs;

import soenudp.UDPException;

public class DTRSMain {
   
    /* 
     * You should always start the replicas 1st
     * then the Leader. The leader is the one with
     * id = 001. He will broadcast to his group
     * a NEW_LEADER command allowing to record their
     * leader address. (This is for simplicity and to
     * conform with the bully algorithm). Since no one
     * know about ip address in the beginning the leader
     * will boradcast NEW_LEADER command. Replica will be 
     * able to get his ip from the datagram packet header)
     * 
     * Else, if leader is NOT started 1st, then replica will
     * receives their leader's address when a transaction
     * is received from the front-end. This imply that the
     * Front-End must already know about the leaders ip. Else he
     * ask the group (so it's always safer to start replica 1st).
     *
     * Example to start to start the MTL group and the view
     * management.
     *
     * You will need to open 4 DOS Prompt (each of them can be on different computers)
     * 
     * Go to the 'bin\' directory: (workspace\TheNameOfYourEclipseProject\bin)
     * 
     * From the command line: (In the proper order)
     * 
     * java soendtrs.DTRSMain 999
     * java soendtrs.DTRSMain 003
     * java soendtrs.DTRSMain 002 
     * java soendtrs.DTRSMain 001  
     * 
     * The start the View Console: 
     * 
     * java soendtrs.TestView    -
     * 
     * - Now wait 10-15 seconds, the view will appear
     * - Close a window. The view will change, and you hear: beep, beep, beep! (wait 10 - 15 secs)
     *                          
     */
    
    /* arg[0] = MTL/TOR, args[1] = 001 ... 00n -> Node ID, 999 as Node ID is a Passive .*/
    
  public static void main(String[] args) {
    DTRSInstance oneDTRS = new DTRSInstance();
    int id;
    String type; // MTL or TOR -> args[0]
    if (args.length > 0) type = args[0];
    // Use MTL by default just to be able to start it without any command line argument
    else type = "MTL";

    if (args.length > 1) id = Integer.parseInt(args[1]);
    else id = 1; // Leader if not comand line argument are passed

    try {
      oneDTRS.startDTRS(type, id);
    } catch (UDPException e) {
      e.printStackTrace();
    }
  }
}
